package com.qqqq.web;

import com.alibaba.fastjson2.JSONObject;
import com.qqqq.core.AjaxResult;
import com.qqqq.core.BaseHttpServlet;
import com.qqqq.dao.impl.StudentDaoImpl;
import com.qqqq.domain.Student;
import com.qqqq.service.StudentService;
import com.qqqq.service.impl.StudentServiceImpl;
import com.qqqq.utils.RequestUtils;
import com.qqqq.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class StuLoginServlet extends BaseHttpServlet {
    private StudentService studentService = new StudentServiceImpl(new StudentDaoImpl());

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject json = RequestUtils.getReqJsonBody(req, JSONObject.class);
        executeBusiness(resp, response -> {
            Student student = studentService.login(json.getInteger("stu_number"), json.getString("password"));
            ResponseUtils.responseJson(resp, AjaxResult.success(student));
        });
    }
}
